package cn.bonoon.services.plugins;

import java.util.Collections;
import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bonoon.core.info.AnnouncementInfo;
import cn.bonoon.core.plugins.AnnouncementService;
import cn.bonoon.entities.plugins.AnnouncementEntity;
import cn.bonoon.kernel.support.IOperator;
import cn.bonoon.kernel.support.services.AbstractService;

@Service
@Transactional(readOnly = true)
public class AnnouncementServiceImpl extends AbstractService<AnnouncementEntity> implements AnnouncementService {

	static final String ql = "select new " + AnnouncementInfo.class.getName()
			+ "(x.createAt, x.content) from AnnouncementEntity x order by createAt desc";

	@Override
	public List<AnnouncementInfo> read(int page, int size) {
		TypedQuery<AnnouncementInfo> query = __query(AnnouncementInfo.class, ql);
		int skip = page * size;
		return query.setFirstResult(skip).setMaxResults(size).getResultList();
	}

	@Override
	public List<AnnouncementInfo> read(IOperator opt, int len) {
		// TODO Auto-generated method stub
		return Collections.emptyList();
	}

	@Override
	public List<AnnouncementInfo> read(String key, IOperator opt, int len) {
		// TODO Auto-generated method stub
		return Collections.emptyList();
	}

	@Override
	public List<AnnouncementEntity> byKey(String key) {
		String ql = "from AnnouncementEntity x where x.key=? order by x.createAt desc";
		TypedQuery<AnnouncementEntity> tqSelect = __query(AnnouncementEntity.class, ql, key);
		tqSelect.setMaxResults(5);
		return tqSelect.getResultList();
//		String ql = "from AnnouncementEntity x where x.key=? order by x.createAt desc";
//		return __list(AnnouncementEntity.class, ql, key);
	}

	@Override
	public List<AnnouncementEntity> byKey(String key, int page) {
		String ql = "from AnnouncementEntity x where x.key=? order by x.createAt desc";
		TypedQuery<AnnouncementEntity> tqSelect = __query(AnnouncementEntity.class, ql, key);
		tqSelect.setFirstResult(page * 5);
		tqSelect.setMaxResults(5);
		return tqSelect.getResultList();
	}

//	@Override
//	public List<AnnouncementEntity> outside_annoucement() {
//		String ql = "from AnnouncementEntity x where x.type=0 order by x.createAt desc";
//		return __list(AnnouncementEntity.class, ql);
//	}
//
//	@Override
//	public List<AnnouncementEntity> inside_annoucement() {
//		String ql = "from AnnouncementEntity x where x.type=1 order by x.createAt desc";
//		return __list(AnnouncementEntity.class, ql);
//	}
//
//	@Override
//	public List<AnnouncementEntity> inside_annoucement(Long page) {
//		String ql = "from AnnouncementEntity x where x.type=1 order by x.createAt desc";
//		TypedQuery<AnnouncementEntity> tqSelect = __query(AnnouncementEntity.class, ql);
//		tqSelect.setFirstResult((int) ((page - 1) * 5));
//		tqSelect.setMaxResults(5);
//		return tqSelect.getResultList();
//	}
//
//	@Override
//	public List<AnnouncementEntity> outside_annoucement(Long page) {
//		// TODO Auto-generated method stub
//		return null;
//	}

}
